<!DOCTYPE html>
<html debug="true">
  <head>
    <link rel="stylesheet" type="text/css" href="../../../../ext/resources/css/ext-all.css" />

    <script type="text/javascript" src="../../../../ext/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="../../../../ext/ext-all-debug.js"></script>

    <script type="text/javascript" src="../../../../openlayers/lib/Firebug/firebug.js"></script>
    <script type="text/javascript" src="../../../../openlayers/lib/OpenLayers.js"></script>
    <script type="text/javascript" src="../../../../geoext/lib/GeoExt.js"></script>

    <script type="text/javascript">
      // Because of a bug in Firefox 2 we need to specify the MapFish base path.
      // See https://bugzilla.mozilla.org/show_bug.cgi?id=351282
      var gMfLocation = "../../../../mapfish/";
    </script>
    <script type="text/javascript" src="../../../../mapfish/MapFish.js"></script>

    <link rel="stylesheet" type="text/css" href="style.css" />

    <script type="text/javascript"><!--
        function test_constructor(t) {
            t.plan(5);

            /*
             * Set up
             */

            var df, p;

            p = new mapfish.Protocol.MapFish();

            /*
             * Test
             */

            // 1 test
            df = new mapfish.widgets.recenter.DataField({protocol: p});
            t.ok(df.protocol == p,
                 "ctor sets the protocol it is given in the instance");

            // 4 tests
            df = new mapfish.widgets.recenter.DataField({url: "foo", limit: 20});
            t.ok(df.protocol instanceof mapfish.Protocol.MapFish,
                 "ctor creates a MapFish protocol if it is given a url");
            t.eq(df.protocol.url, "foo",
                 "ctor creates MapFish protocol with the correct url");
            t.eq(df.protocol.params.no_geom, true,
                 "ctor creates MapFish protocol with correct no_geom param");
            t.eq(df.protocol.params.limit, 20,
                 "ctor creates MapFish protocol with correct limit param");
        }

        function test_render(t) {
            t.plan(6);

            /*
             * Set up
             */

            var df, combo;

            df = new mapfish.widgets.recenter.DataField({
                url: "foo",
                queryParam: "bar",
                minChars: 4
            });

            /*
             * Test
             */

            df.render("widget");
            combo = df.items.first();
            t.ok(combo instanceof Ext.form.ComboBox,
                 "the widget contains a combo box");
            t.eq(combo.queryParam, "bar",
                 "the combo is configured with expected queryParam");
            t.ok(combo.store.reader instanceof GeoExt.data.FeatureReader,
                 ["the combo is configured with a store configured",
                  "with a feature reader"].join(" "));
            t.ok(combo.store.proxy instanceof GeoExt.data.ProtocolProxy,
                 ["the combo is configured with a store configured",
                  "with a protocol proxy"].join(" "));
            t.ok(combo.store.proxy.protocol instanceof OpenLayers.Protocol,
                 ["the combo is configured with a store configured",
                  "with a protocol proxy configured with a protocol"].join(" "));
            t.eq(combo.minChars, 4,
                 "the combo is configured with expected minChars");
        }

        function test_load(t) {
            t.plan(5);

            /*
             * Set up
             */

            var df, combo, q;

            df = new mapfish.widgets.recenter.DataField({
                url: "foo", queryParam: "bar",
            });
            df.render("widget");
            combo = df.items.first();

            /*
             * Test
             */
            
            q = "aaa";

            combo.store.proxy.protocol.read = function(options) {
                t.eq(options.params, {},
                     "protocol.read is passed no params");
                t.ok(options.filter instanceof OpenLayers.Filter.Comparison,
                     "protocol.read is passed a comparison filter");
                t.eq(options.filter.type, OpenLayers.Filter.Comparison.LIKE,
                     "protocol.read is passed a LIKE filter");
                t.eq(options.filter.property, "bar",
                     "protocol.read is passed a filter with expected property");
                t.eq(options.filter.value, ".*" + q + ".*",
                     "protocol.read is passed a filter with expected value");
            };

            combo.doQuery(q);
        }

        function test_onComboSelect(t) {
            t.plan(7);

            /*
             * Set up
             */

            var df, record, geom, bounds;
            var recenterOnCoords = 0, recenterOnBbox = 0;

            df = new mapfish.widgets.recenter.DataField({
                url: "foo"
            });

            record = {
                get: function() {
                    return new OpenLayers.Feature.Vector(geom);
                }
            };

            df.recenterOnCoords = function(x, y) {
                t.eq(x, geom.x, "recenterOnCoords called with expected x");
                t.eq(y, geom.y, "recenterOnCoords called with expected x");
                recenterOnCoords++;
            };

            df.recenterOnBbox = function(_bounds) {
                t.ok(_bounds == bounds,
                     "recenterOnBbox called with expected bounds");
                recenterOnBbox++;
            };

            /*
             * Test
             */

            // OpenLayers.Geometry.Point and defaultZoom unset
            // 2 tests
            geom = new OpenLayers.Geometry.Point(0.1, 0.2);
            bounds = geom.getBounds();
            df.onComboSelect(record);
            t.eq(recenterOnBbox, 1, "recenterOnBbox called");

            // OpenLayers.Geometry.Point and defaultZoom set
            // 3 tests
            geom = new OpenLayers.Geometry.Point(0.1, 0.2);
            df.defaultZoom = 3;
            df.onComboSelect(record);
            t.eq(recenterOnCoords, 1, "recenterOnCoords called");

            // not OpenLayers.Geometry.Point
            // 2 tests
            geom = OpenLayers.Geometry.fromWKT(
                "POLYGON((0 0, 0 4, 4 4, 4 0, 0 0),(1 1, 1 3, 3 3, 3 1, 1 1))"
            );
            bounds = geom.getBounds();
            df.onComboSelect(record);
            t.eq(recenterOnBbox, 2, "recenterOnBbox called");
        }
    --></script>

  <body>
    <div id="widget"></div>
  </body>
</html>
